package org.example.demofa;

import org.example.properties.MyProperties1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * 其中 @RestController 等同于 （@Controller 与 @ResponseBody）
 */
//@RestController
//@SpringBootApplication
//@ComponentScan(basePackages = {"org.example.properties"})
public class DemofaApplication {

    @Autowired
    private  MyProperties1 myProperties1;//这里是需要注释的。

    public static void main(String[] args) {
        SpringApplication.run(DemofaApplication.class, args);
    }


    @GetMapping("/pro")
    public MyProperties1 myProperties1() {
        /**
         * \@SpringBootApplication注释包含@ComponentScan，但是它只扫描和当前类同一包中的文件。
         * \因为MyProperties1在另一个包所以要加上 @ComponentScan(basePackages = {"org.example.properties"})
         * \否者便会报错
         * \而且这样也可以访问properties包中的内容
         */
        System.out.println("===================================================");
        System.out.println(myProperties1.toString());
        System.out.println("===================================================");
        return myProperties1;
    }

    @GetMapping("/demo")
    public String demo(){
        return "hello spring boot world";
    }

    @Bean
    public CommandLineRunner commandLineRunner (ApplicationContext ctx) {
        //目的是：
        return args -> {
            System.out.println("SpringBoot 提供的 Bean");
            String [] beanNames = ctx.getBeanDefinitionNames();
            Arrays.sort(beanNames);
            Arrays.stream(beanNames).forEach(System.out::println);
        };
    }
}

